package javaidiot;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class View {

    private static final Scanner SN = new Scanner(System.in);

    public static String out(int s) {
        return String.valueOf(s);
    }

    public static String out(String s) {
        return s;
    }

    public String printArray(String[][] array) {
        String tmp = "";
        if (array != null) {
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    tmp += array[i][j] + ((j < (array[i].length - 1)) ? " : " : "\n");
                }
            }
        } else {
            tmp = "Null";
        }
        return out(tmp);
    }

    public String exception(String ex) {
        String message = "";
        if (ex.equals("not integer")) {
            message = out("...very polite message that given input is definetly not an integer");
        }
        if (ex.endsWith("out of range")) {
            message = out("...very polite message that given input is definetly incorrect");
        }
        if (ex.endsWith("not float")) {
            message = out("...very polite message that given input is definetly incorrect");
        }
        return message;
    }

    public String error(String s) {
        String message = "";
        if (s.equalsIgnoreCase("empty field")) {
            message = "Obligatory field was not filled. No data will be saved.";
        }
        return message;
    }

    public String dateNicer(String d) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        String error = "";
        try {
            date = (Date) dateFormat.parse(d);
        } catch (ParseException e) {
            error = "---------- --:--";
        }
        return (!error.isEmpty()) ? error : dateFormat.format(date);
    }

    public String[] getProductListArray(String[][] a) {
        String[] s = new String[a.length];
        if (a[0][0] != null) {
            for (int i = 0; i < a.length; i++) {
                s[i] = a[i][1];
            }
        }
        return s;
    }

    public String[] getEmployeeListArray(String[][] a) {
        String[] s = new String[a.length];
        if (a[0][0] != null) {
            for (int i = 0; i < a.length; i++) {
                s[i] = a[i][1] + " " + a[i][2];
            }
        }
        return s;
    }

    public String[] getOrderListArray(String[][] a) {
        String[] s = new String[a.length];
        if (a[0][0] != null) {
            for (int i = 0; i < a.length; i++) {
                s[i] = dateNicer(a[i][1])
                        + ((a[i][2] != null) ? " by " + a[i][2] : " not assigned");
            }
        }
        return s;
    }

    public String getOrderProducts(String[][] a) {
        String s = "\n";
        if (a[0][0] != null) {
            for (int i = 0; i < a.length; i++) {
                s += a[i][1] + " : " + a[i][2] + "\n";
            }
        }
        return s;
    }

    public String getOrderInfo(String[][] a) {
        String s = "";

        if (a[0][0] != null) {
            s += "Order " + a[0][5] + "\n";
            s += "at " + dateNicer(a[0][4]) + "\n\n";
            s += "Products:\n";
            for (int i = 0; i < a.length; i++) {
                s += a[i][1] + " : " + a[i][2] + " pcs.\n";

            }
            s += "\nResponsible person:\n";
            s += a[0][3];
        }
        
        return s;
    }

    public String[] getIdsArray(String[][] a) {
        String[] s = new String[a.length];
        for (int i = 0; i < a.length; i++) {
            s[i] = a[i][0];
        }
        return s;
    }

    public String[] getNameArray(String[][] a, int n) {
        String[] s = new String[a.length];
        if (a[0][0] != null) {
            for (int i = 0; i < a.length; i++) {
                s[i] = a[i][n];
            }
        }
        return s;
    }
}